package com.robot.dao.feigndto.context;
import lombok.Builder;
import lombok.Data;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class RequestContext {

    private static final ThreadLocal<ConcurrentMap<String, Object>> contextHolder = ThreadLocal.withInitial(ConcurrentHashMap::new);
    private static final String CONTEXT_KEY_UserAccountInfoCtx = "UserAccountInfoCtx";
    @Builder
    @Data
    public static class UserAccountInfoCtx{
        Long accountID; //积分账户ID
        Long userID; // 用户ID
        String componyCode; // 公司编码
    }
    public static void setUserAccountInfoCtx( UserAccountInfoCtx value) {
        Object putRes = contextHolder.get().put(CONTEXT_KEY_UserAccountInfoCtx, value);
    }

    public static UserAccountInfoCtx getUserAccountInfo() {
        UserAccountInfoCtx res= (UserAccountInfoCtx)contextHolder.get().get(CONTEXT_KEY_UserAccountInfoCtx);
        // todo 禁止上线，测试用
        if (res==null){
            System.out.println("getUserAccountInfo,未获取到用户id！！");
            res=new RequestContext.UserAccountInfoCtx(18L,19L,"");
        }else {
            System.out.println("用户ID获取成功,res="+res);
        }
        return res;
    }



    public static void remove() {
        contextHolder.remove();
    }
}